import { RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle } from '@angular/router';
import {LocalStorage } from "./local.storage";
export class SimpleReuseStrategy implements RouteReuseStrategy {

    public static handlers:any = {};
    /** 表示对所有路由允许复用 如果你有路由不想利用可以在这加一些业务逻辑判断 */
    public shouldDetach(route: ActivatedRouteSnapshot): boolean {
        // console.log(route)
        if(route.data.key){
            return true;
        }else{
            return false;
        }
        // return false;
    }

    /** 当路由离开时会触发。按path作为key存储路由快照&组件当前实例对象 */
    public store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
        // console.log(route)
        console.log(route.data.key)
        SimpleReuseStrategy.handlers[route.data.key] = handle
    }

    /** 若 path 在缓存中有的都认为允许还原路由 */
    public shouldAttach(route: ActivatedRouteSnapshot): boolean {
        return !!route.data && !!SimpleReuseStrategy.handlers[route.data.key]
    }

    /** 从缓存中获取快照，若无则返回null */
    public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
        if (!route.data) {
            return null
        }
        
        return SimpleReuseStrategy.handlers[route.data.key]
    }

    /** 进入路由触发，判断是否同一路由 */
    public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
       // console.log(future,curr)
        return future.routeConfig === curr.routeConfig
    }
    static deleteCache(str){
        delete SimpleReuseStrategy.handlers[str]
    }
}